package com.itstyle.quartz.job;

import com.itstyle.quartz.entity.*;
import com.itstyle.quartz.service.IDetailsBeanService;
import com.itstyle.quartz.service.IMogudingService;
import com.itstyle.quartz.service.ISignInLogService;
import com.itstyle.quartz.service.IUserinfoService;
import com.itstyle.quartz.utils.ApplicationContextUtil;
import com.itstyle.quartz.utils.DateUtil;
import org.quartz.*;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * 实现序列化接口、防止重启应用出现quartz Couldn't retrieve job because a required class was not found 的问题
 * Job 的实例要到该执行它们的时候才会实例化出来。每次 Job 被执行，一个新的 Job 实例会被创建。
 * 其中暗含的意思就是你的 Job 不必担心线程安全性，因为同一时刻仅有一个线程去执行给定 Job 类的实例，甚至是并发执行同一 Job 也是如此。
 *
 * @DisallowConcurrentExecution 保证上一个任务执行完后，再去执行下一个任务，这里的任务是同一个任务
 */
@DisallowConcurrentExecution
public class ChickenJob implements Job, Serializable {

    private static final long serialVersionUID = 1L;

    @Override
    public void execute(JobExecutionContext context) {
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap dataMap = jobDetail.getJobDataMap();
        /**
         * 获取任务中保存的方法名字，动态调用方法
         */
        String methodName = dataMap.getString("jobMethodName");
        try {
            ChickenJob job = new ChickenJob();
            Method method = job.getClass().getMethod(methodName);
            method.invoke(job);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void test1() {
        System.out.println("测试方法1");
    }




    /* 签到功能 */

    public void qiandao() {
        IUserinfoService userinfoService = (IUserinfoService) ApplicationContextUtil.getBean("UserinfoImpl");
        IDetailsBeanService detailsBeanService = (IDetailsBeanService) ApplicationContextUtil.getBean("DetailsBeanImpl");
        IMogudingService mogudingService = (IMogudingService) ApplicationContextUtil.getBean("MogudingImpl");
        ISignInLogService signInLogService = (ISignInLogService) ApplicationContextUtil.getBean("SignInLogImpl");


        try {
            Result listUserinfo = userinfoService.listUserinfo(1, 10);
            PageBean<Userinfo> ListPage = (PageBean<Userinfo>) listUserinfo.get("msg");
            List<Userinfo> listUser = ListPage.getPageData();
            DetailsBean detailsBean = new DetailsBean();
            SignInLog log = new SignInLog();

            for (Userinfo userinfo : listUser) {
                detailsBean.setUserId(userinfo.getUserId());
                Result result = detailsBeanService.listDetailsBean(detailsBean, 1, 10);
                PageBean<DetailsBean> msg = (PageBean<DetailsBean>) result.get("msg");
                if (msg.getPageData().size() > 0) {
                    detailsBean = msg.getPageData().get(0);
                    Map<String, String> authorization1 = mogudingService.login(userinfo);
                    String authorization = authorization1.get("Authorization");
                    String userId=authorization1.get("userId");
                    if (authorization == null) {
                        String time = DateUtil.getNowTime();//获取String类型的时间
                        log.setUsername(userinfo.getUsername());
                        log.setAddress(detailsBean.getAddress());
                        log.setState("密码错误");
                        log.setLogtime(time);
                        signInLogService.save(log);
                        continue;
                    }
                    String planId = mogudingService.planId(authorization,userId);
                    Result sin = mogudingService.sin(authorization, planId, detailsBean,userId);

                    String time = DateUtil.getNowTime();//获取String类型的时间
                    log.setUsername(userinfo.getUsername());
                    log.setAddress(detailsBean.getAddress());
                    log.setState(sin.get("msg").toString());
                    log.setLogtime(time);
                    signInLogService.save(log);
                } else {
                    String time = DateUtil.getNowTime();//获取String类型的时间
                    log.setUsername(userinfo.getUsername());
                    log.setAddress(detailsBean.getAddress());
                    log.setState("没有填写用户详情");
                    log.setLogtime(time);
                    signInLogService.save(log);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("-----------------------签到异常----------------------");
        }
    }
}
